package com.example.Wcf;
import java.io.*;
import java.util.Dictionary;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;

import com.example.usingjsontocallwcf.LoaiHang;

import android.R.integer;
public class ConnectWcf {
	
	public String Connect_Wcf(String adress,String keyPara,String valuePara,HttpClient httpclient) throws JSONException, ClientProtocolException, IOException
	{
		HttpPost httpget = new HttpPost(adress);
		httpget.setHeader("Accept", "application/json");
		httpget.setHeader("Content-type", "application/json");
		
		ConverObjectToJsonString jsonString=new 	ConverObjectToJsonString();
		String nameString = null;
			nameString=jsonString.GetJsonFromString(keyPara, valuePara);
			httpget.setEntity( new StringEntity(nameString));
		    
					HttpResponse httpResponse = httpclient.execute(httpget);
					
					 HttpEntity httpEntity = httpResponse.getEntity();
			        	InputStream stream=httpEntity.getContent();
			        return convertStreamToString(stream);
	}
	public String Connect_Wcf(String adress,HttpClient httpClient) throws JSONException, ClientProtocolException, IOException, IllegalArgumentException, IllegalAccessException
	{
		HttpPost httpget = new HttpPost(adress);
		httpget.setHeader("Accept", "application/json");
		httpget.setHeader("Content-type", "application/json");
		
		
		
		
					HttpResponse httpResponse = httpClient.execute(httpget);
					
					 HttpEntity httpEntity = httpResponse.getEntity();
			        	InputStream stream=httpEntity.getContent();
			        return convertStreamToString(stream);
	}
	public String Connect_Wcf(String adress,Dictionary<String, String>para,HttpClient httpClient) throws JSONException, ClientProtocolException, IOException
	{
		HttpPost httpget = new HttpPost(adress);
		httpget.setHeader("Accept", "application/json");
		httpget.setHeader("Content-type", "application/json");
		
		ConverObjectToJsonString jsonString=new 	ConverObjectToJsonString();
		String nameString = null;
			nameString=jsonString.GetJsonFromString(para);
			httpget.setEntity( new StringEntity(nameString));
		
					HttpResponse httpResponse = httpClient.execute(httpget);
					
					 HttpEntity httpEntity = httpResponse.getEntity();
			        	InputStream stream=httpEntity.getContent();
			        return convertStreamToString(stream);
	}
	public String Connect_WcfObject(String adress,Dictionary<String, JSonFactory>para,HttpClient httpClient) throws JSONException, ClientProtocolException, IOException, IllegalArgumentException, IllegalAccessException
	{
		HttpPost httpget = new HttpPost(adress);
		httpget.setHeader("Accept", "application/json");
		httpget.setHeader("Content-type", "application/json");
		
		ConverObjectToJsonString jsonString=new 	ConverObjectToJsonString();
		String nameString = null;
			nameString=jsonString.GetJsonFromObject(para);
			httpget.setEntity( new StringEntity(nameString));
		
					HttpResponse httpResponse = httpClient.execute(httpget);
					
					 HttpEntity httpEntity = httpResponse.getEntity();
			        	InputStream stream=httpEntity.getContent();
			        return convertStreamToString(stream);
	}
	public String Connect_Wcf(String adress,String keyPara,JSonFactory s,HttpClient httpClient) throws IllegalArgumentException, JSONException, IllegalAccessException, IllegalStateException, IOException 
	{
		HttpPost httpget = new HttpPost(adress);
		httpget.setHeader("Accept", "application/json");
		httpget.setHeader("Content-type", "application/json");
		
		ConverObjectToJsonString jsonString=new 	ConverObjectToJsonString();
		String nameString = null;
			nameString=jsonString.GetJsonFromObject(s,keyPara).toString();
			httpget.setEntity( new StringEntity(nameString));
		
					HttpResponse httpResponse = httpClient.execute(httpget);
					
					 HttpEntity httpEntity = httpResponse.getEntity();
			        	InputStream stream=httpEntity.getContent();
			        return convertStreamToString(stream);
	}
	public String Connect_Wcf(String adress,Dictionary<String, JSonFactory>para,Dictionary<String, String>paras,HttpClient httpClient) throws IllegalArgumentException, JSONException, IllegalAccessException, IllegalStateException, IOException 
	{
		HttpPost httpget = new HttpPost(adress);
		httpget.setHeader("Accept", "application/json");
		httpget.setHeader("Content-type", "application/json");
		
		ConverObjectToJsonString jsonString=new 	ConverObjectToJsonString();
		String nameString = null;
			nameString=jsonString.GetJsonFromObject(paras, para);
			httpget.setEntity( new StringEntity(nameString));
		
					HttpResponse httpResponse = httpClient.execute(httpget);
					
					 HttpEntity httpEntity = httpResponse.getEntity();
			        	InputStream stream=httpEntity.getContent();
			        return convertStreamToString(stream);
	}
	
	private  String convertStreamToString(InputStream is)
    {
    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    	StringBuilder sb = new StringBuilder();

    	String line = null;
    	try {
    		while ((line = reader.readLine()) != null) {
    			sb.append(line + "\n");
    		}
    	}
    	catch (IOException e) {
    		e.printStackTrace();
    	}
    	finally {
    		try {
    			is.close();
    		} catch (IOException e) {
				e.printStackTrace();
    		}
    	}
    	return sb.toString();
    }

}
